package com.minshenglife.zanzan.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.dao.V_deptDao;
import com.minshenglife.zanzan.dao.V_empDao;
import com.minshenglife.zanzan.entity.Dept;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.eoa.V_DEPT;
import com.minshenglife.zanzan.entity.eoa.V_EMP;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.service.SyncInfoService;


/**
 * 部门员工信息同步service
 * @author huodd
 *
 */
@Component
@Transactional
public class SyncInfoServiceImpl implements SyncInfoService {
	Logger log = LoggerFactory.getLogger(SyncInfoServiceImpl.class);
	
	@Resource
	UserDao userDao;
	@Resource
	V_deptDao v_deptDao;
	@Resource
	V_empDao v_empDao;
	
	@Override
	public void initSyncDeptInfo() {
		log.info("初始部门信息开始。。。");
		List<V_DEPT> vList=v_deptDao.findByExample(null);
		int index=0;
		if(vList.size()>0){
			for(V_DEPT vd :vList){
				System.out.println(vd.getDeptId());
				Dept dept=new Dept();
				dept.setDept_id(vd.getDeptId());
				dept.setAbbr_name(vd.getAbbrName());
				dept.setDept_name(vd.getDeptName());
				dept.setParentNodeId(vd.getParentNodeId());
				dept.setIsActive(vd.getIsActive());
				dept.setNodeStatus(vd.getNodeStatus());
				dept.setBu(vd.getBU());
				dept.setDept_type(vd.getDeptType());
				dept.setDept_level(vd.getDeptLevel());
				dept.setIs_cadre(vd.getIsCadre());
				dept.setIs_virtual(vd.getIsVirtrual());
				dept.setIs_company(vd.getIsCompany());
				dept.setDept_quality(vd.getDeptQuality());
				try {
					userDao.insertDeptInfo(dept);
				} catch (Exception e) {
					log.error(vd.getAbbrName()+"---"+vd.getDeptId()+"添加失败");
					e.printStackTrace();
				}
				index++;
			}
			System.out.println("========insert success rows:"+index);
		}
		log.info("初始部门信息结束");
	}

	@Override
	public void initSyncEmpInfo() {
		log.info("初始员工信息开始。。。");
			List<V_EMP> vList=v_empDao.findByExample(null);//page.getRows();//
			if(vList.size()>0){
				for(V_EMP ve :vList){
					User user=new User();
					user.setEmpId(ve.getEmpId());
					user.setEmp_name(ve.getEmpName());
					user.setEmpType(ve.getEmployeeType());
					user.setEmp_status(ve.getEmp_status());
					user.setDept_id(ve.getDeptId());
					user.setSex(ve.getGender());
					user.setIdNo(ve.getIdNo());
					user.setBirthday(ve.getBirthday());
					user.setStature(ve.getStature());
					user.setWeight(ve.getWeight());
					user.setBlood_type(ve.getBlood_type());
					user.setHire_date(ve.getHire_date());
					user.setTelephone(ve.getTelephone());
					user.setMobilePhone(ve.getMobilePhone());
					user.setWork_email(ve.getWorkMail());
					user.setUser_state(Keys.USER_STATE_ZERO);
					user.setUser_type(Keys.USER_TYPE_ZERO);
					user.setIntegralNum(new Long(0));
					user.setRedFlowerNum(new Double(0));
					user.setFdFlowerNum(new Double(0));
					user.setSendFlowerNum(new Long(0));
					user.setCanSendNum(new Long(0));
					user.setCanTuCaoNum(new Long(0));
					try {
						userDao.add(user);
					} catch (Exception e) {
						log.error(ve.getEmpName()+"--"+ve.getEmpId()+"添加失败");
						e.printStackTrace();
					}
				}
			}
			log.info("初始员工信息结束。。。");
	}
	
	@Override
	public void syncDeptInfo() {
		log.info("同步部门信息开始。。。"+new Date());
		long millisStart = System.currentTimeMillis();
		List<V_DEPT> vList=v_deptDao.findByExample(null);
		int index=0;
		if(vList.size()>0){
			for(V_DEPT vd :vList){
				System.out.println(vd.getDeptId());
				Dept dept=userDao.getDeptInfo(vd.getDeptId());
				if(dept!=null){//修改
					if(vd.getAbbrName()!=null && !vd.getAbbrName().equals(dept.getAbbr_name()))
						dept.setAbbr_name(vd.getAbbrName());
					if(vd.getDeptName()!=null && !vd.getDeptName().equals(dept.getDept_name()))
						dept.setDept_name(vd.getDeptName());
					if(vd.getParentNodeId()!=null && !vd.getParentNodeId().equals(dept.getParentNodeId()))
						dept.setParentNodeId(vd.getParentNodeId());
					if(vd.getIsActive()!=null && !vd.getIsActive().equals(dept.getIsActive()))
						dept.setIsActive(vd.getIsActive());
					if(vd.getNodeStatus()!=null && !vd.getNodeStatus().equals(dept.getNodeStatus()))
						dept.setNodeStatus(vd.getNodeStatus());
					if(vd.getBU()!=null && !vd.getBU().equals(dept.getBu()))
						dept.setBu(vd.getBU());
					if(vd.getDeptType()!=null && !vd.getDeptType().equals(dept.getDept_type()))
						dept.setDept_type(vd.getDeptType());
					if(vd.getDeptLevel()!=null && !vd.getDeptLevel().equals(dept.getDept_level()))
						dept.setDept_level(vd.getDeptLevel());
					if(vd.getIsCadre()!=null && !vd.getIsCadre().equals(dept.getIs_cadre()))
						dept.setIs_cadre(vd.getIsCadre());
					if(vd.getIsVirtrual()!=null && !vd.getIsVirtrual().equals(dept.getIs_virtual()))
						dept.setIs_virtual(vd.getIsVirtrual());
					if(vd.getIsCompany()!=null && !vd.getIsCompany().equals(dept.getIs_company()))
						dept.setIs_company(vd.getIsCompany());
					if(vd.getDeptQuality()!=null && !vd.getDeptQuality().equals(dept.getDept_quality()))
						dept.setDept_quality(vd.getDeptQuality());
					try {
						userDao.updateDeptInfo(dept);
					} catch (Exception e) {
						log.error(vd.getAbbrName()+"---"+vd.getDeptId()+"修改失败");
						e.printStackTrace();
					}
				}else{//新增
					dept=new Dept();
					dept.setDept_id(vd.getDeptId());
					dept.setAbbr_name(vd.getAbbrName());
					dept.setDept_name(vd.getDeptName());
					dept.setParentNodeId(vd.getParentNodeId());
					dept.setIsActive(vd.getIsActive());
					dept.setNodeStatus(vd.getNodeStatus());
					dept.setBu(vd.getBU());
					dept.setDept_type(vd.getDeptType());
					dept.setDept_level(vd.getDeptLevel());
					dept.setIs_cadre(vd.getIsCadre());
					dept.setIs_virtual(vd.getIsVirtrual());
					dept.setIs_company(vd.getIsCompany());
					dept.setDept_quality(vd.getDeptQuality());
					try {
						userDao.insertDeptInfo(dept);
					} catch (Exception e) {
						log.error(vd.getAbbrName()+"---"+vd.getDeptId()+"添加失败");
						e.printStackTrace();
					}
				}
				index++;
			}
			System.out.println("======== success rows:"+index);
		}
		long millisEnd = System.currentTimeMillis();
		long g = (millisEnd - millisStart)/1000;
		log.info("同步部门信息结束，共用时"+g+"秒");
		
	}

	@Override
	public void syncEmpInfo() {
		log.info("同步员工信息开始。。。"+new Date());
		long millisStart = System.currentTimeMillis();
		List<V_EMP> vList=v_empDao.findByExample(null);//page.getRows();//
		if(vList.size()>0){
			for(V_EMP ve :vList){
				User userTemp = new User();
				userTemp.setEmpId(ve.getEmpId());
//				User user=userDao.getUserByUserId(ve.getEmpId());
				User user=null;
				List<User> users = userDao.findByExampleAll(userTemp);
				if(users != null && users.size() > 0){
					user = users.get(0);
				}
				if(user!=null){
					if(ve.getEmpName()!=null && !ve.getEmpName().equals(user.getEmp_name()))
						user.setEmp_name(ve.getEmpName());
					if(ve.getEmployeeType()!=null && !ve.getEmployeeType().equals(user.getEmpType()))
						user.setEmpType(ve.getEmployeeType());
					if(ve.getEmp_status()!=null && !ve.getEmp_status().equals(user.getEmp_status()))
						user.setEmp_status(ve.getEmp_status());
					if(ve.getDeptId()!=null && !ve.getDeptId().equals(user.getDept_id()))
						user.setDept_id(ve.getDeptId());
					if(ve.getGender()!=null && !ve.getGender().equals(user.getSex()))
						user.setSex(ve.getGender());
					if(ve.getIdNo()!=null && !ve.getIdNo().equals(user.getIdNo()))
						user.setIdNo(ve.getIdNo());
					if(ve.getBirthday()!=null && !ve.getBirthday().equals(user.getBirthday()))
						user.setBirthday(ve.getBirthday());
					if(ve.getStature()!=null && !ve.getStature().equals(user.getStature()))
						user.setStature(ve.getStature());
					if(ve.getWeight()!=null && !ve.getWeight().equals(user.getWeight()))
						user.setWeight(ve.getWeight());
					if(ve.getBlood_type()!=null && !ve.getBlood_type().equals(user.getBlood_type()))
						user.setBlood_type(ve.getBlood_type());
					if(ve.getHire_date()!=null && !ve.getHire_date().equals(user.getHire_date()))
						user.setHire_date(ve.getHire_date());
					if(ve.getTelephone()!=null && !ve.getTelephone().equals(user.getTelephone()))
						user.setTelephone(ve.getTelephone());
					if(ve.getMobilePhone()!=null && !ve.getMobilePhone().equals(user.getMobilePhone()))
						user.setMobilePhone(ve.getMobilePhone());
					if(ve.getWorkMail()!=null && !ve.getWorkMail().equals(user.getWork_email()))
						user.setWork_email(ve.getWorkMail());
					/**add by huodd,添加同步职级信息,at date 20140826*/
					if(ve.getPost_id()!=null && !ve.getPost_id().equals(user.getPostId()))
						user.setPostId(ve.getPost_id());
					if(ve.getPosiName()!=null && !ve.getPosiName().equals(user.getPosiName()))
						user.setPosiName(ve.getPosiName());
					
					try {
						userDao.update(user);
					} catch (Exception e) {
						log.error(ve.getEmpName()+"--"+ve.getEmpId()+"修该失败");
						e.printStackTrace();
					}
				}else{
					user=new User();
					user.setEmpId(ve.getEmpId());
					user.setEmp_name(ve.getEmpName());
					user.setEmpType(ve.getEmployeeType());
					user.setEmp_status(ve.getEmp_status());
					user.setDept_id(ve.getDeptId());
					user.setSex(ve.getGender());
					user.setIdNo(ve.getIdNo());
					user.setBirthday(ve.getBirthday());
					user.setStature(ve.getStature());
					user.setWeight(ve.getWeight());
					user.setBlood_type(ve.getBlood_type());
					user.setHire_date(ve.getHire_date());
					user.setTelephone(ve.getTelephone());
					user.setMobilePhone(ve.getMobilePhone());
					user.setWork_email(ve.getWorkMail());
					user.setUser_state(Keys.USER_STATE_ZERO);
					user.setUser_type(Keys.USER_TYPE_ZERO);
					user.setIntegralNum(new Long(0));
					user.setRedFlowerNum(new Double(0));
					user.setFdFlowerNum(new Double(0));
					user.setSendFlowerNum(new Long(0));
					user.setCanSendNum(new Long(0));
					user.setCanTuCaoNum(new Long(0));
					/**add by huodd,添加同步职级信息,at date 20140826*/
					user.setPostId(ve.getPost_id());
					user.setPosiName(ve.getPosiName());
					try {
						userDao.add(user);
					} catch (Exception e) {
						log.error(ve.getEmpName()+"--"+ve.getEmpId()+"添加失败");
						e.printStackTrace();
					}
				}
				
			}
		}
		long millisEnd = System.currentTimeMillis();
		long g = (millisEnd - millisStart)/1000;
		log.info("同步员工信息结束。。。,共用时秒"+g+"");
		
	}

	@SuppressWarnings("unused")
	private Page<V_EMP> findVEmpList(Map<String, Object> map, int pageNo,
			int pageSize) {
		if(map==null)
			map=new HashMap<String,Object>();
		int offset = (pageNo-1) * pageSize;
        Long count =v_empDao.count(null);
        map.put("from", offset);
        map.put("to", offset+pageSize);
        List<V_EMP> rows =v_empDao.findBy(map);
        Page<V_EMP> ret = new Page<V_EMP>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
        return ret;
	}

	
	
}
